import axios from "@/axios"

export function getArticleDetail(articleId) {
    return axios.post("/admin/article/detail", {articleId})
}

export function publishArticle(data) {
    // return axios.post("/admin/article/publish", data)
    // 处理数据，确保titleImage是字符串
    const formattedData = {
        ...data,
        titleImage: typeof data.titleImage === 'object' ?
            (data.titleImage.url || data.titleImage.src || '') :
            data.titleImage
    };

    // 如果是空对象，设置为空字符串
    if (formattedData.titleImage && typeof formattedData.titleImage === 'object' && Object.keys(formattedData.titleImage).length === 0) {
        formattedData.titleImage = '';
    }

    console.log('格式化后的发布数据:', formattedData);
    return axios.post("/admin/article/publish", formattedData)
}

export function getArticlePageList(data) {
    return axios.post("/admin/article/list", data)
}

export function deleteArticle(articleId) {
    return axios.post("/admin/article/delete", {articleId})
}

export function updateArticle(data) {
    // 同样处理titleImage字段
    const formattedData = {
        ...data,
        titleImage: typeof data.titleImage === 'object' ?
            (data.titleImage.url || data.titleImage.src || '') :
            data.titleImage
    };

    if (formattedData.titleImage && typeof formattedData.titleImage === 'object' && Object.keys(formattedData.titleImage).length === 0) {
        formattedData.titleImage = '';
    }

    return axios.post("/admin/article/update", formattedData)
}
